import { _decorator, Component, EventTouch, Input, input, instantiate, Node, Prefab, RigidBody, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Test')
export class Test extends Component {

@property
public maxAttackNum:Number = 20

@property
public bulletSpeed:Number = 50

@property
public testString:string='test'

@property(Prefab)
public bulletPrefab:Prefab=null

@property(Node)
public BulletParent:Node=null

@property
public FireRate:number=0.3

private FireTimer = 0;

private isTouch = false;

start() {
input.on(Input.EventType.TOUCH_START,this.onTouchStart,this)
input.on(Input.EventType.TOUCH_END,this.onTouchEnd,this)
}
onTouchStart(event: EventTouch){
    this.isTouch=true;
}

onTouchEnd(event: EventTouch){
    this.isTouch=false;
    }

update(deltaTime: number) {
    if(this.isTouch){
        this.FireTimer+=deltaTime
        if (this.FireTimer>this.FireRate){
            this.fire()
            this.FireTimer = 0;
        }
    }
}
fire(){
    const bullet = instantiate(this.bulletPrefab);
    bullet.setParent(this.BulletParent)
    // bullet.setPosition(1,3.5,25)
    bullet.setWorldPosition(this.node.position)
    const rgd = bullet.getComponent(RigidBody)
    rgd.setLinearVelocity(new Vec3(0,0,-this.bulletSpeed))
}
}